//initWindow里win赋值后调用setTray(win)
const {
	Tray,
	nativeImage,
	Menu,
	app
} = require('electron');
const isMac = process.platform === 'darwin'
const path = require('path')
let tray = null

const setTray = function (win) {
	const image = path.join(__dirname, './static/yflogo.ico');
	if (isMac) {
		image.setTemplateImage(true)
	}
	tray = new Tray(image)
	let contextMenu = Menu.buildFromTemplate([{
		label: '显示',
		click: () => {
			winShow(win)
		}
	}, {
		label: '退出',
		click: () => {
			setTimeout(() => {
				app.exit();
			}, 30);
		}
	}])
	if (!isMac) {
		tray.on('click', () => {
			winShow(win)
		})
	}
	tray.setToolTip('雅方家庭教育')
	tray.setContextMenu(contextMenu)
	win.on('close', (e) => {
		console.log(win.isFocused(), 'win.isFocused()');
		e.preventDefault(); //阻止窗口的关闭事件
		win.hide();
	})

	//闪烁图标

	// var count = 0;

	// var timer = setInterval(function() {
	// 	count++;

	// 	if (count % 2 == 0) {
	// 		console.log(count);
	// 		tray.setImage(path.join(__dirname, '../static/yflogo.ico'))

	// 	} else {

	// 		tray.setImage(path.join(__dirname, '../static/empty.ico'))
	// 	}

	// }, 500)
}

function winShow(win) {
	if (win.isVisible()) {
		if (win.isMinimized()) {
			win.restore()
			win.focus()
		} else {
			win.focus()
		}
	} else {
		!isMac && win.minimize()
		win.show()
		win.setSkipTaskbar(false)
	}
}
module.exports = setTray;
